package ar.edu.itba.it.pdc.httpParser;

import java.nio.ByteBuffer;
import java.util.Queue;

import ar.edu.itba.it.pdc.httpParser.interfaces.ImputCharGetter;
import ar.edu.itba.it.pdc.nio.ByteBufferPool;

public class ByteBufferQueueImputCharGetter implements ImputCharGetter {

	private Queue<ByteBuffer> in;
	
	private boolean end;
	
	public ByteBufferQueueImputCharGetter(Queue<ByteBuffer> bq){
		in = bq;
	}
	
	@Override
	public boolean end() {
		return end;
	}

	@Override
	public Byte read() {
		
		while(!in.isEmpty() && in.peek().remaining() == 0){
			ByteBufferPool.getInstance().retriveByteBuffer(in.remove());
		}
		
		if(in.isEmpty()){
			return null;
		}
		
		byte b = in.peek().get();
		if(in.peek().position() == in.peek().limit()){
			ByteBufferPool.getInstance().retriveByteBuffer(in.remove());
		}
		return b;
	}
	

}
